type rules = Record<string, rulesItem[]>
interface rulesItem {
  message: string
  required: boolean
  minLength: number
  maxLength: number
  pattern: RegExp
  validator: Function
}

export default {
  setLocal(name: string, data: any) {
    uni.setStorageSync(name, data)
  },
  getLocal(name: string) {
    return uni.getStorageSync(name)
  },
  deleteLocal(name: string) {
    uni.removeStorageSync(name)
  },
  hasLocal(name: string) {
    const res = uni.getStorageInfoSync()
    return res.keys.includes(name)
  },
  clearLocal() {
    uni.clearStorageSync()
  },
  typeOf(obj: any): any {
    const toString: any = Object.prototype.toString
    const map: any = {
      '[object Boolean]': 'boolean',
      '[object Number]': 'number',
      '[object String]': 'string',
      '[object Function]': 'function',
      '[object Array]': 'array',
      '[object Date]': 'date',
      '[object RegExp]': 'regExp',
      '[object Undefined]': 'undefined',
      '[object Null]': 'null',
      '[object Object]': 'object'
    }
    return map[toString.call(obj)]
  },
  deepClone<T>(data: T): Promise<T> {
    return new Promise((resolve) => {
      const { port1, port2 } = new MessageChannel()
      port1.postMessage(data)
      port2.onmessage = (msg) => {
        resolve(msg.data)
      }
    })
  },
  valid(rules: rules, form: Record<string, any>) {
    //表单验证
    for (const key in rules) {
      for (let i = 0; i < rules[key].length; i++) {
        if (rules[key][i].required) {
          if (form[key] === '' || form[key] === null) {
            uni.showToast({
              icon: 'none',
              title: rules[key][i].message
            })
            return false
          }
          if (form[key] instanceof Array && !form[key].length) {
            uni.showToast({
              icon: 'none',
              title: rules[key][i].message
            })
            return false
          }
        } else if (rules[key][i].pattern) {
          if (!rules[key][i].pattern.test(form[key])) {
            uni.showToast({
              icon: 'none',
              title: rules[key][i].message
            })
            return false
          }
        } else if (rules[key][i].minLength) {
          if (form[key].length < rules[key][i].minLength) {
            uni.showToast({
              icon: 'none',
              title: rules[key][i].message
            })
            return false
          }
        } else if (rules[key][i].maxLength) {
          if (form[key].length > rules[key][i].maxLength) {
            uni.showToast({
              icon: 'none',
              title: rules[key][i].message
            })
            return false
          }
        } else if (rules[key][i].validator) {
          if (typeof rules[key][i].validator === 'function') {
            if (!rules[key][i].validator()) {
              return false
            }
          }
        }
      }
    }
    return true
  },
  calculateTotalPages(totalRecords: number, recordsPerPage: number) {
    return Math.ceil(totalRecords / recordsPerPage)
  }
}
